package com.zhixiang.xaec.action.bpmndemo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.zhixiang.core.command.QueryFilter;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.util.String2Array;
import com.zhixiang.core.util.YearGridCrud;
import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.bpmndemo.LeaveBill;
import com.zhixiang.xaec.entity.safebase.weixianzuoye.WeiXianZuoYeGuanLi;
import com.zhixiang.xaec.service.bpmndemo.LeaveBillService;
import com.zhixiang.xaec.service.bpmndemo.WorkflowService;

/**
 * @author gao
 * @since 2017年1月16日
 * 
 */
@Controller
@Scope("prototype")
public class LeaveBillAction extends BaseAction
{
    private LeaveBill leaveBill;
    
    
    public LeaveBill getLeaveBill()
    {
        return leaveBill;
    }

    public void setLeaveBill(LeaveBill leaveBill)
    {
        this.leaveBill = leaveBill;
    }

    @Autowired
    private LeaveBillService leaveBillService ; 
    
    @Autowired
    private WorkflowService workflowService ; 
    
    private static final long serialVersionUID = 1L;

       public String list(){
            QueryFilter localQueryFilter=new QueryFilter(getRequest());
            List <LeaveBill>list= leaveBillService.getAll(localQueryFilter);
            setTotalCounts(localQueryFilter.getPagingBean().getTotalItems());
            setResult(list);
            return SUCCESS;
        }
        
        public String get(){
            String id=getRequest().getParameter("id");
            if(StringUtils.isNotEmpty(id)){
            setResult(leaveBillService.get(Long.valueOf(id)));    
            }
            return SUCCESS;
        }
        
        public String delete(){
            try
            {
                String ids[]=getRequest().getParameterValues("ids");
                Long[] longIds = String2Array.stringArrayToLongArray(ids);
                leaveBillService.remove(longIds);
            }
            catch (Exception e)
            {
                setMsg("删除失败");
                setSuccess(false);
            }
            return SUCCESS;
        }
       
        public String save()
        {
            try
            {
                leaveBillService.save(leaveBill);
            }
            catch (Exception e)
            {
                setMsg("保存失败");
                setSuccess(false);
            }
            
            return SUCCESS;
        }
        
        public String startProcess()
        {
            String businessId=getRequest().getParameter("businessId");
            if(StringUtils.isNotEmpty(businessId)){
                try
                {
                    LeaveBill lb=leaveBillService.get(Long.valueOf(businessId));
                    Map<String, Object> map=new HashMap<String, Object>();
                    map.put("days", lb.getDays());
                    workflowService.saveStartProcess(businessId, LeaveBill.class, map);
                    lb.setStatus("2");
                    leaveBillService.save(lb);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    setSuccess(false);
                    setMsg("流程变量："+e.getMessage().substring(e.getMessage().indexOf("{")+1,e.getMessage().indexOf("}"))+"没有设置值！");
                }
              
            }else{
                setSuccess(false);
                setMsg("请假单Id不能为空！");
            }
            return SUCCESS;
        }
        public String processNextStep(){
            String businessId=getRequest().getParameter("businessId");
            String instanceId=getRequest().getParameter("instanceId");
            String taskId=getRequest().getParameter("taskId");
            String message=getRequest().getParameter("message");
            String comment=getRequest().getParameter("comment");
            try
            {   
                LeaveBill lb=  leaveBillService.get(Long.valueOf(businessId));
                Map<String, Object> map=new HashMap<String, Object>();
                map.put("days", lb.getDays());
                Integer r=workflowService.processForward(instanceId, taskId, message, comment,LeaveBill.class, map);
                if(r==-1){
                    setSuccess(false);
                    setMsg("任务已被办理！");
                }else {
                    if(r==0){
                        lb.setStatus("3");
                        leaveBillService.save(lb);
                    }
                    setSuccess(true);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                setSuccess(false);
                setMsg("流程变量："+e.getMessage().substring(e.getMessage().indexOf("{")+1,e.getMessage().indexOf("}"))+"没有设置值！");
            
            }
            
            
            return SUCCESS;
        }
        
        public String findInstanceIdByBusinessId(){
            String businessId=getRequest().getParameter("businessId");
            if(StringUtils.isNotEmpty(businessId)){
               String instanceId= workflowService.findInstanceIdByBusinessId(businessId,"LeaveBill");
               setResult(instanceId);
            }
           return SUCCESS;
            
        }

}
